#!/bin/sh
set -e
export LC_ALL=C

type=$1
preversion=$2

checkpkgver () {
    local status pkg
    pkg=$1
    status=$(dpkg -s $pkg 2>/dev/null | grep ^Status: | sed -e 's/^Status: \(.*\) \(.*\) \(.*\)/\3/g')
    if [ -n "$status" ] && [ "$status" != "not-installed" ] && [ "$status" != "config-files" ]; then
	echo $(dpkg -s $pkg 2>/dev/null | grep ^Version: | sed -e 's/^Version: *//');
    fi
}

if [ "$type" = "configure" ]
then
    # /etc/ld.so.nohwcap code:
    __NOHWCAP__
fi

if [ "$type" = configure ]
then
    # Load debconf module if available
    if [ -f /usr/share/debconf/confmodule ] ; then
	. /usr/share/debconf/confmodule
    fi

    if [ -n "$preversion" ] && [ -x "$(command -v ischroot)" ] && ! ischroot; then
	if dpkg --compare-versions "$preversion" lt GLIBC_VERSION; then
	    check="apache2.2-common apache apache-ssl apache-perl autofs at"
	    check="$check boa cucipop courier-authdaemon cron cups exim"
	    check="$check exim4-base dovecot-common cucipop incron lprng lpr"
	    check="$check lpr-ppd mysql-server nis openbsd-inetd"
	    check="$check openldapd openssh-server postgresql-common proftpd"
	    check="$check postfix postfix-tls rsync samba sasl2-bin slapd"
	    check="$check smail sendmail snmpd spamassassin vsftpd"
	    check="$check wu-ftpd wu-ftpd-academ wwwoffle"
	    check="$check webmin dropbear gdm"
	    # NSS services check: 
	    __NSS_CHECK__
	    if [ -n "$services" ]; then

	        if [ -f /usr/share/debconf/confmodule ] ; then
		    db_version 2.0
		    db_input critical libraries/restart-without-asking || true
		    db_go || true
		    db_get libraries/restart-without-asking
		    if [ "$RET" != true ]; then
			db_reset glibc/restart-services
			db_set glibc/restart-services "$services"
			db_input critical glibc/restart-services || true
			db_go || true
			db_get glibc/restart-services
			if [ "x$RET" != "x" ]
			then
			    services="$RET"
			else
			    services=""
			fi
		    fi
		else
		    echo
		    echo "Name Service Switch update in the C Library: post-installation question."
		    echo
		    echo "Running services and programs that are using NSS need to be restarted,"
		    echo "otherwise they might not be able to do lookup or authentication any more"
		    echo "(for services such as ssh, this can affect your ability to login)."
		    echo "Note: restarting sshd/telnetd should not affect any existing connections."
		    echo
		    echo "The services detected are: "
		    echo "        $services"
		    echo
		    echo "If other services have begun to fail mysteriously after this upgrade, it is"
		    echo "probably necessary to restart them too.  We recommend that you reboot your"
		    echo "machine after the upgrade to avoid NSS-related troubles."
		    echo
		    frontend=`echo "$DEBIAN_FRONTEND" | tr '[:upper:]' '[:lower:]'`
		    if [ "$frontend" = noninteractive ]; then
		    	echo "Non-interactive mode, restarting services"
		    	answer=yes
		    else
		    	echo -n "Do you wish to restart services? [Y/n] "
		    	read answer
		    	case $answer in
			    N*|n*) services="" ;;
			    *) ;;
		        esac
		    fi
		fi
		echo
		if [ "$services" != "" ]; then
		    echo "Restarting services possibly affected by the upgrade:"
		    failed=""
		    for service in $services; do
			case "$service" in
			    gdm)
				echo -n "  $service: reloading..."
				if invoke-rc.d ${service} reload > /dev/null 2>&1; then
				    echo "done."
				else
				    echo "FAILED! ($?)"
				    failed="$service $failed"
				fi
				;;
			    *)
				echo -n "  $service: restarting..."
				if invoke-rc.d ${service} restart > /dev/null 2>&1; then
				    echo "done."
				else
				    echo "FAILED! ($?)"
				    failed="$service $failed"
				fi
				;;
			esac
		    done
		    echo
		    if [ -n "$failed" ]; then
	        	if [ -f /usr/share/debconf/confmodule ] ; then
		    	    db_fset glibc/restart-failed seen false
			    db_subst glibc/restart-failed services "$failed"
			    db_input critical glibc/restart-failed || true
			    db_go || true
			else
			    echo "The following services failed to start: $failed"
			    echo
			    echo "You will need to start these manually by running \`invoke-rc.d <service> start'"
			    echo "If the service still fails to start, you may need to file a bug on"
			    echo "${DPKG_MAINTSCRIPT_PACKAGE}:${DPKG_MAINTSCRIPT_ARCH} or the service involved."
			    frontend=`echo "$DEBIAN_FRONTEND" | tr '[:upper:]' '[:lower:]'`
			    if [ "$frontend" != noninteractive ]; then
			    	echo
			    	echo -n "Press ENTER to continue: "
			    	read foo
			    fi
			fi
		    else
			echo "Services restarted successfully."
                    fi
		fi
                # Shut down the frontend, to make sure none of the
		# restarted services keep a connection open to it
	        if [ -f /usr/share/debconf/confmodule ] ; then
		    db_stop
		fi
	    else
		echo "Nothing to restart."
	    fi
	fi # end upgrading and $preversion lt GLIBC_VERSION
    fi # Upgrading

    # Restart init.  Currently handles chroots, systemd and upstart, and
    # assumes anything else is going to not fail at behaving like
    # sysvinit:
    TELINIT=yes
    if ischroot 2>/dev/null; then
        # Don't bother trying to re-exec init from a chroot:
        TELINIT=no
    elif [ -d /run/systemd/system ]; then
        # Restart systemd on upgrade, but carefully.
        # The restart is wanted because of LP: #1942276 and Bug: #993821
        # The care is needed because of https://bugs.debian.org/753725
        # (if systemd --help fails the system might still be quite broken but
        # that seems better than the kernel panic that results if systemd
        # cannot reexec itself).
        TELINIT=no
        if systemd --help >/dev/null 2>/dev/null; then
            systemctl daemon-reexec
        else
            echo "Error: Could not restart systemd, systemd binary not working" >&2
        fi
    fi
    if [ "$TELINIT" = "yes" ]; then
        telinit u 2>/dev/null || true ; sleep 1
    fi
fi

#DEBHELPER#

exit 0
